package com.test.deencredit.analytics_deencredit.data_sdk_deencredit.task_deencredit;

import android.content.Context;
import android.text.TextUtils;

import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.beans_deencredit.ASRequestPms_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.http_deencredit.ASReqstManger_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.logger_deencredit.ASLger_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.services_deencredit.ASBtryIfo_deencredit;
import com.test.deencredit.config_deencredit.CountryConfig_deencredit;
import com.test.deencredit.config_deencredit.Paths_deencredit;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

public class ASDevcBtyTsk_deencredit implements Supplier<String> {

    private final static String TAG = "ASDeviceBatteryTask";

    private final String mTaskId;
    private final Context mContext;

    public ASDevcBtyTsk_deencredit(Context context, String taskId) {
        this.mContext = context;
        this.mTaskId = taskId;
    }

    @Override
    public String get() {
        // 日志标记
        ASLger_deencredit.d_deencredit(TAG, TAG + " 执行中...");

        // 过滤条件
        if (TextUtils.isEmpty(Paths_deencredit.PATH_BATTERY_deencredit)) {
            return mTaskId + "";
        }

        // 创建任务
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CountDownLatch latch = new CountDownLatch(1);
        AsyncBtyTsk_deencredit asyncTask = new AsyncBtyTsk_deencredit(mContext, latch, mTaskId);
        executorService.submit(asyncTask);

        // 等待任务完成
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return mTaskId + "";
    }

    static class AsyncBtyTsk_deencredit implements Runnable {
        private final CountDownLatch latch;
        private final Context mContext;
        private final String mTaskId;

        AsyncBtyTsk_deencredit(Context context, CountDownLatch latch, String taskId) {
            this.mContext = context;
            this.latch = latch;
            this.mTaskId = taskId;
        }

        @Override
        public void run() {
            ASBtryIfo_deencredit batteryInfo = new ASBtryIfo_deencredit();
            batteryInfo.getBatteryInfo_deencredit(mContext, batteryInfoBean -> {
                ASRequestPms_deencredit netParams = new ASRequestPms_deencredit();
                netParams.setDBG_deencredit(batteryInfoBean);
//                String result = new Gson().toJson(netParams).trim();
                String result = netParams.toEncryptParams_deencredit(Paths_deencredit.PATH_BATTERY_deencredit);

                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                    String url = CountryConfig_deencredit.getInstance_deencredit().getCommonPath_deencredit();
                    new ASReqstManger_deencredit.Builder_deencredit().setReqId_deencredit(this.mTaskId).setUrl_deencredit(url).setBody_deencredit(result).build_deencredit().doPostRqst_deencredit();

                    return mTaskId + "";
                });

                future.thenRun(latch::countDown);
            });

        }
    }
}
